<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhangyh.fun","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="WebPack 介绍1. 前言 WebPack 是一个前端资源加载、打包工具。根据模块的依赖关系进行静态分析，然后将这些模块按照指定的规则生成对应的静态资源。">
<meta property="og:type" content="article">
<meta property="og:title" content="WebPack 介绍">
<meta property="og:url" content="https://zhangyh.fun/2021/08/13/WebPack%20%E4%BB%8B%E7%BB%8D/index.html">
<meta property="og:site_name" content="XiaoPang の Blog">
<meta property="og:description" content="WebPack 介绍1. 前言 WebPack 是一个前端资源加载、打包工具。根据模块的依赖关系进行静态分析，然后将这些模块按照指定的规则生成对应的静态资源。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-13T01:40:00.000Z">
<meta property="article:modified_time" content="2021-08-17T02:15:06.905Z">
<meta property="article:author" content="XiaoPang">
<meta property="article:tag" content="nodejs">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://zhangyh.fun/2021/08/13/WebPack%20%E4%BB%8B%E7%BB%8D/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>WebPack 介绍 | XiaoPang の Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?54b35e78bac318b678b24fbb33049203";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


</head>

<body itemscope itemtype="http://schema.org/WebPage">

  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">XiaoPang の Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
    <script src="https://v1.hitokoto.cn/?c=f&encode=js&select=%23hitokoto" defer></script>

      <p class="site-subtitle" itemprop="description" id="hitokoto"><span id="hitokoto_text">记录生活中的点点滴滴</span></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">11</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">34</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhangyh.fun/2021/08/13/WebPack%20%E4%BB%8B%E7%BB%8D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="XiaoPang">
      <meta itemprop="description" content="少说，多做">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XiaoPang の Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          WebPack 介绍
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于：</span>

              <time title="创建时间：2021-08-13 09:40:00" itemprop="dateCreated datePublished" datetime="2021-08-13T09:40:00+08:00">2021-08-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于：</span>
                <time title="修改时间：2021-08-17 10:15:06" itemprop="dateModified" datetime="2021-08-17T10:15:06+08:00">2021-08-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/WebPack/" itemprop="url" rel="index"><span itemprop="name">WebPack</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <br>
            <span class="post-meta-item" title="本文字数">
            <span class="post-meta-item-icon">
            <i class="fa fa-file-word"></i>
            </span>
           <span class="post-meta-item-text">本文字数：</span>
           <span title="post.wordcount">
                1.1k 字
            </span>
            </span>
            <span class="post-meta-item" title="阅读时长">
            <span class="post-meta-item-icon">
            <i class="fa fa-clock"></i>
            </span>
          <span class="post-meta-item-text">阅读时长：</span>
           <span title="post.min2read">
                4 分钟
            </span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="WebPack-介绍"><a href="#WebPack-介绍" class="headerlink" title="WebPack 介绍"></a><code>WebPack</code> 介绍</h2><h3 id="1-前言"><a href="#1-前言" class="headerlink" title="1. 前言"></a>1. 前言</h3><ul>
<li><code>WebPack</code> 是一个前端资源加载、打包工具。根据模块的依赖关系进行静态分析，然后将这些模块按照指定的规则生成对应的静态资源。<span id="more"></span>
<h3 id="2-安装"><a href="#2-安装" class="headerlink" title="2. 安装"></a>2. 安装</h3></li>
<li>前提条件：本地环境需要支持<code>node.js</code></li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 1. 创建项目目录--&gt;(webpack_demo)可以是任意；</span><br><span class="line"># 2. 使用Webstorm 打开项目目录，进入命令行界面</span><br><span class="line">npm init -y</span><br><span class="line"># 3. 安装webpack及webpack-cli</span><br><span class="line">npm install webpack webpack-cli --D</span><br><span class="line"># 4. 创建src 目录或根据需要创建其他子目录</span><br><span class="line"># 5. 在src下创建一些js文件和一个主入口文件index.js</span><br><span class="line"># 6. 控制台运行命令</span><br><span class="line">开发环境：webpack --mode=development</span><br><span class="line">生产环境：webpack --mode=production</span><br><span class="line"># 7. 可以使用node运行打包资源，也可使用HTML引入打包资源</span><br></pre></td></tr></table></figure>

<h3 id="3-webpack-config-js-文件配置"><a href="#3-webpack-config-js-文件配置" class="headerlink" title="3. webpack.config.js 文件配置"></a>3. webpack.config.js 文件配置</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">1. entry</span><br><span class="line"># 入口（entry）指 webpack 以哪个文件作为起点开始打包，分析构建内部依赖图。默认是src/index.js</span><br><span class="line">2. output</span><br><span class="line"># 输出（output）指 webpack打包后的资源 bundles 输出到哪里，以及如何命名。默认是dist/main.js</span><br><span class="line">3. loader</span><br><span class="line"># loader 让 webpack 能够处理那些非 JavaScript、css、img等。将他们处理成webpack 能够识别的资源，可以理解为一个翻译过程。（默认webpack自身只能理解js和json）</span><br><span class="line">4. plugins</span><br><span class="line"># 插件（plugins）可用于执行范围更广的任务。插件范围包括，从打包和压缩，一直到重新定义环境中的变量</span><br><span class="line">5. mode</span><br><span class="line"># 模式（mode）指示webpack 使用相应模式的配置。</span><br><span class="line"># 开发模式（development）: 配置比较简单，创建代码本地调试运行的环境</span><br><span class="line"># 生产模式（production）: 代码需要不断优化达到性能最好，生成代码上线运行的环境</span><br><span class="line"># 注: 都会自动启用一些插件，生产模式使用插件更多。</span><br></pre></td></tr></table></figure>
<h4 id="3-1-使用"><a href="#3-1-使用" class="headerlink" title="3.1 使用"></a>3.1 使用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;resolve&#125; = <span class="built_in">require</span>(<span class="string">&quot;path&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">// 生产模式：production，开发模式：development</span></span><br><span class="line">    <span class="attr">mode</span>: <span class="string">&quot;production&quot;</span>,</span><br><span class="line">    <span class="comment">/* 举例：</span></span><br><span class="line"><span class="comment">    * 1. 单入口。str，打包成一个chunk，输出一个bundle 文件，chunk的名称默认</span></span><br><span class="line"><span class="comment">    entry: &quot;./src/index.js&quot;</span></span><br><span class="line"><span class="comment">    * 2. 多入口。list，所有入口文件形成一个chunk(名称默认)</span></span><br><span class="line"><span class="comment">    entry: [&quot;./src/one.js&quot;, &quot;./src/two.js&quot;]</span></span><br><span class="line"><span class="comment">    * 3. 多入口。obj，几个入口就有几个chunk，并输出几个bundle 文件，chunk的名称是key.js</span></span><br><span class="line"><span class="comment">    entry: &#123;</span></span><br><span class="line"><span class="comment">        one: &quot;./src/one.js&quot;,</span></span><br><span class="line"><span class="comment">        two: &quot;./src/two.js&quot;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    * 4. 特殊用法。之前模式混合</span></span><br><span class="line"><span class="comment">    entry: &#123;</span></span><br><span class="line"><span class="comment">        * 数组中所有入口文件生成一个chunk，输出bundle文件，名称是key</span></span><br><span class="line"><span class="comment">        one_two: [&quot;./src/one.js&quot;, &quot;./src/two.js&quot;]</span></span><br><span class="line"><span class="comment">        * 形成一个chunk，输出一个bundle文件</span></span><br><span class="line"><span class="comment">        index: &quot;./src/index.js&quot;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    * */</span></span><br><span class="line">    <span class="attr">entry</span>: <span class="string">&quot;./src/index.js&quot;</span>,</span><br><span class="line">    <span class="attr">output</span>: &#123;</span><br><span class="line">        <span class="attr">filename</span>: <span class="string">&quot;build.js&quot;</span>,</span><br><span class="line">        <span class="attr">path</span>: resolve(__dirname, <span class="string">&quot;build&quot;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">module</span>: &#123;</span><br><span class="line">        <span class="attr">rules</span>: [</span><br><span class="line"></span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">plugins</span>: [</span><br><span class="line">        </span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-webPack打包html-资源，压缩html代码"><a href="#4-webPack打包html-资源，压缩html代码" class="headerlink" title="4. webPack打包html 资源，压缩html代码"></a>4. webPack打包html 资源，压缩html代码</h3><h4 id="4-1-打包单个html-文件"><a href="#4-1-打包单个html-文件" class="headerlink" title="4.1 打包单个html 文件"></a>4.1 打包单个html 文件</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用插件（html-webpack-plugin）对html文件进行处理</span></span><br><span class="line"><span class="comment">// 1. 下载</span></span><br><span class="line">npm i html-webpack-plugin -D</span><br><span class="line"><span class="comment">// i --&gt; 安装</span></span><br><span class="line"><span class="comment">// -D --&gt; 表示开发环境运行</span></span><br><span class="line"><span class="comment">// 2. 引用插件</span></span><br><span class="line"><span class="comment">// 在webpack.config.js 中引用。</span></span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&quot;html-webpack-plugin&quot;</span>)</span><br><span class="line"><span class="comment">// 3. 使用插件</span></span><br><span class="line"><span class="attr">plugins</span>: [</span><br><span class="line">	<span class="comment">//1. 默认创建空的HTML文件，自动引入打包资源（js/css）</span></span><br><span class="line">	<span class="comment">// new HtmlWebpackPlugin()</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">// 通过参数可以输出有结构的HTML资源</span></span><br><span class="line">	<span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">		<span class="comment">// 复制 &quot;./src/index.html&quot;，并自动引入打包输出的所有资源（js/css）</span></span><br><span class="line">		<span class="attr">template</span>: <span class="string">&quot;./src/index.html&quot;</span>,</span><br><span class="line">		<span class="comment">// 默认输出index.html，设置filename设置输出文件名称</span></span><br><span class="line">		<span class="attr">filename</span>: <span class="string">&quot;demo.html&quot;</span>,</span><br><span class="line">		<span class="comment">// 压缩html 代码</span></span><br><span class="line">		<span class="attr">minify</span>: &#123;</span><br><span class="line">			<span class="comment">// 移除空格</span></span><br><span class="line">			<span class="attr">collapseWhitespace</span>: <span class="literal">true</span>,</span><br><span class="line">			<span class="comment">// 移除注释</span></span><br><span class="line">			<span class="attr">removeComments</span>: <span class="literal">true</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h4 id="4-2-打包多个html-文件"><a href="#4-2-打包多个html-文件" class="headerlink" title="4.2 打包多个html 文件"></a>4.2 打包多个html 文件</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 多个entry</span></span><br><span class="line"><span class="attr">entry</span>: &#123;</span><br><span class="line">	<span class="attr">verdor</span>: [<span class="string">&quot;jquery&quot;</span>, <span class="string">&quot;./src/js/common.js&quot;</span>],</span><br><span class="line">	<span class="attr">index</span>: <span class="string">&quot;./src/js/index.js&quot;</span>,</span><br><span class="line">	<span class="attr">cart</span>: <span class="string">&quot;./src/js/cart.js&quot;</span></span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 负责打包html文件，将js注入html，minify 压缩 html</span></span><br><span class="line"><span class="attr">plugins</span>: [</span><br><span class="line">	<span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">		<span class="attr">filename</span>: <span class="string">&quot;index.html&quot;</span>,</span><br><span class="line">		<span class="attr">template</span>: <span class="string">&quot;./src/index.html&quot;</span>,</span><br><span class="line">		<span class="comment">// chunks 对应html文件，所需的资源。默认全部资源导入</span></span><br><span class="line">		<span class="attr">chunks</span>: [<span class="string">&quot;index&quot;</span>, <span class="string">&quot;vendor&quot;</span>],</span><br><span class="line">		<span class="attr">minify</span>: &#123;</span><br><span class="line">			<span class="attr">removeComment</span>: <span class="literal">true</span>,</span><br><span class="line">			<span class="attr">collapseWhitespace</span>: <span class="literal">true</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;),</span><br><span class="line">	<span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">		<span class="attr">filename</span>: <span class="string">&quot;cart.html&quot;</span>,</span><br><span class="line">		<span class="attr">template</span>: <span class="string">&quot;./src/cart.html&quot;</span>,</span><br><span class="line">		<span class="attr">chunks</span>: [<span class="string">&quot;cart&quot;</span>, <span class="string">&quot;vendor&quot;</span>]</span><br><span class="line">	&#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="5-webpack-打包-css-资源"><a href="#5-webpack-打包-css-资源" class="headerlink" title="5. webpack 打包 css 资源"></a>5. webpack 打包 css 资源</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 安装插件（css-loader/style-loader）</span></span><br><span class="line">npm i css-loader style-loader -D</span><br><span class="line"><span class="comment">// css-loader: 处理css中的@import 和 url 这样的外部资源</span></span><br><span class="line"><span class="comment">// style-loader: 把样式插入到DOM 中，方法是在head 中插入一个style 标签，并把样式写入到这个标签的innerHTML</span></span><br><span class="line"><span class="attr">module</span>: &#123;</span><br><span class="line">    <span class="attr">rules</span>: [</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</span><br><span class="line">			<span class="comment">// 一个规则</span></span><br><span class="line">			<span class="comment">// loader: &quot;css-styl&quot;</span></span><br><span class="line">			<span class="comment">// 多个规则</span></span><br><span class="line">			use: [<span class="string">&quot;style-loader&quot;</span>, <span class="string">&quot;css-loader&quot;</span>]</span><br><span class="line">		&#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>XiaoPang
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zhangyh.fun/2021/08/13/WebPack%20%E4%BB%8B%E7%BB%8D/" title="WebPack 介绍">https://zhangyh.fun/2021/08/13/WebPack 介绍/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://zhangyh.fun/2021/08/13/WebPack%20%E4%BB%8B%E7%BB%8D/"  aria-label="复制成功！"></i></span>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
        });
    });
</script>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/nodejs/" rel="tag"><i class="fa fa-tag"></i> nodejs</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/13/python%20%E6%93%8D%E4%BD%9Credis,%20%E9%9B%86%E5%90%88%EF%BC%88set%EF%BC%89%E7%B1%BB%E5%9E%8B/" rel="prev" title="python 操作redis, 集合（set）类型">
      <i class="fa fa-chevron-left"></i> python 操作redis, 集合（set）类型
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/13/Neo4j%20%E5%AE%89%E8%A3%85/" rel="next" title="Neo4j 安装">
      Neo4j 安装 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    

  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#WebPack-%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">WebPack 介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%89%8D%E8%A8%80"><span class="nav-number">1.1.</span> <span class="nav-text">1. 前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%AE%89%E8%A3%85"><span class="nav-number">1.2.</span> <span class="nav-text">2. 安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-webpack-config-js-%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE"><span class="nav-number">1.3.</span> <span class="nav-text">3. webpack.config.js 文件配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-%E4%BD%BF%E7%94%A8"><span class="nav-number">1.3.1.</span> <span class="nav-text">3.1 使用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-webPack%E6%89%93%E5%8C%85html-%E8%B5%84%E6%BA%90%EF%BC%8C%E5%8E%8B%E7%BC%A9html%E4%BB%A3%E7%A0%81"><span class="nav-number">1.4.</span> <span class="nav-text">4. webPack打包html 资源，压缩html代码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-%E6%89%93%E5%8C%85%E5%8D%95%E4%B8%AAhtml-%E6%96%87%E4%BB%B6"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1 打包单个html 文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-%E6%89%93%E5%8C%85%E5%A4%9A%E4%B8%AAhtml-%E6%96%87%E4%BB%B6"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 打包多个html 文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-webpack-%E6%89%93%E5%8C%85-css-%E8%B5%84%E6%BA%90"><span class="nav-number">1.5.</span> <span class="nav-text">5. webpack 打包 css 资源</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="XiaoPang"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">XiaoPang</p>
  <div class="site-description" itemprop="description">少说，多做</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhang123-123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhang123-123" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/1741290057@qq.com" title="E-Mail → 1741290057@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


<!-- CloudCalendar -->
<div class="widget-wrap" style="width: 90%;margin-left: auto;margin-right: auto; opacity: 0.97;">
	<div class="widget" id="CloudCalendar"></div>
</div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoPang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
    <script type="text/javascript" color="0,0,0" opacity='0.5' zIndex="-2" count="200" src="/js/my_js/canvas-nest.min.js"></script>
    

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/my_js/clicklove.js"></script>

<!-- calendar widget -->

    <script src="/lib/calendar/calendar.min.js"></script>
    <script src="/lib/calendar/languages.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#CloudCalendar').aCalendar('zh-CN',
            $.extend(
                '', {
                    single:true,
                    root:'/calendar/'
                }
            )
        );
    });
    </script>


</html>
